//
//  WBHomeViewController.swift
//  个人微博
//
//  Created by 任聪聪 on 2017/5/16.
//  Copyright © 2017年 任孜聪. All rights reserved.
//

import UIKit
//定义全局常量
fileprivate let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {
    //lazy var staticList = [String]()
    //初始化视图模型类
    fileprivate lazy var viewModels = WBStatusListViewModel()
    @objc fileprivate func showFriends(){
         print("好朋友")
        let vc = WBDemoViewController()
        //vc.hidesBottomBarWhenPushed = true
        //nav 调用push放方法将vc压入栈底
        navigationController?.pushViewController(vc, animated: true)
    }
    override func loadData() {
        print("最后一条数据\(viewModels.statusLists.last?.text)")
        viewModels.loadStatusLists(isPullUp: self.isPullUp){ (isSucess,isRefreshTable) in
            //结束下拉刷新
            self.refresh?.endRefreshing()
            if isRefreshTable{
              self.tv?.reloadData()
            }
            self.isPullUp = false
            print("加载数据完毕")
        }
//        WBNetWorkManager.shared.statusList { (result, isSucess) in
//            print(result)
//            print("加载数据完毕")
//        }
        //延迟下拉刷新
        //尾随闭包，运用主队列进行调度
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
//            for i in 0..<15 {
//                if self.isPullUp {
//                    self.staticList.append("上拉\(i)")
//                }else{
//                    self.staticList.insert("\(i)", at: 0)
//                }
//            }
//            //结束下拉刷新
//            self.refresh?.endRefreshing()
//            self.tv?.reloadData()
//            self.isPullUp = false
//        }
    }
}
extension WBHomeViewController{
    //重写父类方法
    override func setUpUI(){
        super.setUpUI()
        //调用OC代码时候，OC方法无法判断是否可选，所以采用定义类型来解除可选选项
          navBarItem.leftBarButtonItem = UIBarButtonItem(title: "朋友", font: 16, any: self, action: #selector(showFriends))
        tv?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        setNavTitle()
    }
    
    /// 设置首页动态标题方法
    fileprivate func setNavTitle(){
        //用自定义按钮来设置首页标题
        let title = WBNetWorkManager.shared.userAccount.screen_name
        let btn = WBTitleButton(title: title)
        navBarItem.titleView = btn
        btn.addTarget(self, action: #selector(clickBtn(btn:)), for: .touchUpInside)
    }
    @objc func clickBtn(btn:UIButton){
        btn.isSelected = !btn.isSelected
    }
}

// MARK: - 实现基类的数据源方法
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModels.statusLists.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.创建cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        //2.设置cell
        cell.textLabel?.text = viewModels.statusLists[indexPath.row].text
        //3.返回cell
        return cell
    }
}

